// pages/bind_phone/index.js
var config = require('../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.request({
      url: config.HOST+'/user/getUserById',
      data: {
        token: wx.getStorageSync("token")
      },
      success:(res)=>{
        console.log(res)
        wx.setStorageSync("phone", res.data.phone);
        this.setData({
          userInfo: res.data
        })
      }
    })
  },
  bindPhone() {
    let phone = this.data.phone
    let token = wx.getStorageSync("token")
    if (this.data.phone == null || this.data.phone == undefined || this.data.phone.trim() == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return;
    }

    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
    } else {
      wx.request({
        url: config.HOST + '/user/bindingPhone', //仅为示例，并非真实的接口地址
        data: {
          token,
          phone
        },
        success(res) {
          console.log(res.data)
          if(res.data.status==200) {
            wx.showToast({
              title: '绑定成功',
            })
            wx.setStorageSync("phone", phone);
          }else{
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      })
    }
  },
  phoneNum(e) {
    this.data.phone = e.detail.value
  }
})